{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PCANBasic import *\n",
    "import time\n",
    "from ctypes import *\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "LKMotorCMD = c_ubyte\n",
    "\n",
    "LKCMD_GetStatus=LKMotorCMD(0x9A)\n",
    "LKCMD_ClearErr=LKMotorCMD(0x9B)\n",
    "LKCMD_GetStatus2=LKMotorCMD(0x9C)\n",
    "LKCMD_GetStatus3=LKMotorCMD(0x9D)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "class LK_MotorParam (Structure):\n",
    "    _fields_=[(\"ID\",c_uint),\n",
    "              (\"temp\",c_uint),\n",
    "              (\"volt\",c_uint),\n",
    "              (\"speed\", c_int16),\n",
    "              (\"speedControl\",c_int32),\n",
    "              (\"encoder\",c_uint16),\n",
    "              (\"encoderRaw\",c_uint16),\n",
    "              (\"encoderOffset\",c_uint16),\n",
    "              (\"iqControl\",c_int16),\n",
    "              (\"iq\",c_int16),\n",
    "              (\"motorAngle\",c_int64),\n",
    "              (\"circleAngle\",c_uint32),\n",
    "              (\"angleControl\",c_int32),\n",
    "              (\"spindDirection\",c_uint8),\n",
    "              (\"maxSpeed\",c_uint16),\n",
    "              (\"angleIncrement\",c_int32),\n",
    "              (\"Accel\",c_int32),\n",
    "    ]\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "class LK_MotorControl:\n",
    "    PcanHandle = PCAN_USBBUS1\n",
    "    IsFD = False\n",
    "    Bitrate = PCAN_BAUD_1M\n",
    "    DLLFound = False\n",
    "    BitrateFD = b'f_clock_mhz=20, nom_brp=5, nom_tseg1=2, nom_tseg2=1, nom_sjw=1, data_brp=2, data_tseg1=3, data_tseg2=1, data_sjw=1'   \n",
    "    CanID = 0\n",
    "    motorParam=LK_MotorParam(); \n",
    "    \n",
    "    def __init__(self,_ID):\n",
    "\n",
    "        self.CanID=_ID\n",
    "        self.motorParam.ID=_ID\n",
    "          \n",
    "        try:\n",
    "            self.objPCANBasic = PCANBasic()\n",
    "            self.DLLFound = True\n",
    "        except :\n",
    "            print(\"Unable to find the library: PCANBasic.dll !\")\n",
    "            self.DLLFound = False\n",
    "            return\n",
    "\n",
    "        ## Initialization of the selected channel\n",
    "        if self.IsFD:\n",
    "            stsResult = self.objPCANBasic.InitializeFD(self.PcanHandle,self.BitrateFD)\n",
    "        else:\n",
    "            stsResult = self.objPCANBasic.Initialize(self.PcanHandle,self.Bitrate)\n",
    "\n",
    "        if stsResult != PCAN_ERROR_OK:\n",
    "            print(\"Can not initialize. Please check the defines in the code.\")\n",
    "            return      \n",
    "        \n",
    "        \n",
    "    def GetStatus(self):\n",
    "        msgCanMessage = TPCANMsg()\n",
    "        msgCanMessage.ID = 0x140+self.CanID\n",
    "        msgCanMessage.LEN = 8\n",
    "        msgCanMessage.MSGTYPE = PCAN_MESSAGE_STANDARD\n",
    "        msgCanMessage.DATA[0] = LKCMD_GetStatus\n",
    "\n",
    "        self.objPCANBasic.Write(self.PcanHandle, msgCanMessage)\n",
    "        time.sleep(0.01)\n",
    "        stsResult=self.objPCANBasic.Read(self.PcanHandle)\n",
    "        if stsResult[1].DATA[0]==LKCMD_GetStatus:\n",
    "             self.motorParam.temp=stsResult[1].DATA[1]\n",
    "\n",
    "    def MoveOneAxis(self):\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PCANUSB was initialized\n"
     ]
    }
   ],
   "source": [
    "PCAN = PCANBasic()\n",
    "result=PCAN.Initialize(PCAN_USBBUS1 ,PCAN_BAUD_1M)\n",
    "if result !=PCAN_ERROR_OK:\n",
    "    result=PCAN.GetErrorText(result)\n",
    "    print(result[1])\n",
    "else:\n",
    "    print(\"PCANUSB was initialized\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "msgCanMessage = TPCANMsg()\n",
    "msgCanMessage.ID = 0x141\n",
    "msgCanMessage.LEN = 8\n",
    "msgCanMessage.MSGTYPE = PCAN_MESSAGE_STANDARD\n",
    "msgCanMessage.DATA[0] = 0x9A\n",
    "\n",
    "PCAN.Write(PCAN_USBBUS1, msgCanMessage)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "stsResult = PCAN.Read(PCAN_USBBUS1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0 0 0\n"
     ]
    }
   ],
   "source": [
    "print(stsResult[1].DATA[0],stsResult[1].DATA[1],stsResult[1].DATA[2],stsResult[1].DATA[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "PCANUSB was released\n"
     ]
    }
   ],
   "source": [
    "result=PCAN.Uninitialize(PCAN_USBBUS1 )\n",
    "if result!=PCAN_ERROR_OK:\n",
    "    result=PCAN.GetErrorText(result)\n",
    "    print(result[1])\n",
    "else:\n",
    "    print(\"PCANUSB was released\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5 4\n"
     ]
    }
   ],
   "source": [
    "class jointCtrl():\n",
    "    ctrlInstance=0\n",
    "    tbCmdPos=0\n",
    "    tbActPos=0\n",
    "\n",
    "J1=jointCtrl()\n",
    "J2=jointCtrl()\n",
    "\n",
    "J1.ctrlInstance=5\n",
    "J2.ctrlInstance=4\n",
    "\n",
    "print(J1.ctrlInstance,J2.ctrlInstance)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'self.txActPos_J1'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"self.txActPos_J\"+str(1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.15"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b09ec625f77bf4fd762565a912b97636504ad6ec901eb2d0f4cf5a7de23e1ee5"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
